#include "TC5020.h"



extern uint32_t flash_Set_Time;
extern uint32_t current_set_Time;
extern TIM_HandleTypeDef htim2;
static uint8_t digital_model[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void set_current_time(uint32_t second)
{
	uint8_t set_minute = second/60;
	uint8_t set_second = second%60;

	uint32_t set_time = 0;
	set_time = digital_model[set_minute/10];
	set_time = set_time<<8;
	set_time = set_time|digital_model[set_minute%10];

	set_time = set_time<<8;
	set_time = set_time|digital_model[set_second/10];
	set_time = set_time<<8;
	set_time = set_time|digital_model[set_second%10];

	open_sm16306s_led(set_time);
}


void open_sm16306s_led(uint32_t data)
{

	data = ((data>>16)|0xffff0000)&((data<<16)|0x0000ffff);

	uint8_t  j;
	SDI_L;//第一路数�??
	CLK_L;//时钟
	LE_L;
	OE_H;//使能

	for (j = 0; j < 32; j++)
	{
		//先准备好数据
		if (data &0x80000000)
		{
			SDI_H;			//数据1
		}
		else
		{
			SDI_L;			//数据0
		}
		CLK_H;
		data  = (data << 1);//数据左移�??�??
		CLK_L;
	}

	LE_H;//�??始移�??
	LE_L;//拉低移位
	OE_L;//使能输出
}

void write_into_flash(uint32_t variable_to_write)
{
	uint32_t address = 0x0801FC00; // 选择一个在Flash中有效的地址
	uint32_t variable_write = variable_to_write;
	HAL_FLASH_Unlock();

	FLASH_EraseInitTypeDef FlashSet;
	FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
	FlashSet.PageAddress = address;
	FlashSet.NbPages = 1;

	uint32_t PageError = 0;
	HAL_FLASHEx_Erase(&FlashSet, &PageError);

	HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, variable_write);
	HAL_FLASH_Lock();
	if(status == HAL_OK)
	{

	}
}
void read_from_flash()
{
	HAL_FLASH_Unlock();
	flash_Set_Time = *(uint32_t *)0x0801FC00;
	HAL_FLASH_Lock();
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim == &htim2)
	{
		if(current_set_Time > 0)
		{
			current_set_Time--;
			set_current_time(current_set_Time);
		}
	}
}







